home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / gray image 2.1 / vfractal_map.cc < prev    next >
C/C++ Source or Header  |  1995-07-30  |  5KB  |  194 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. //
  3. //    Verify construction of fractal maps and show them off
  4. //
  5. // $Id: vfractal_map.cc,v 2.0 1995/03/21 18:40:16 oleg Exp oleg $
  6.  
  7. #include "fractal_map.h"
  8. #include "filter.h"
  9. #include <iostream.h>
  10. #include <math.h>
  11.  
  12. #ifndef M_PI
  13. #define M_PI _PI
  14. #endif
  15.  
  16. static void test_nonoise(const card order)
  17. {
  18.   cout << "\nTest constructing a map of order " << order << 
  19.       " without any noise\n" << endl;
  20.  
  21.   class NonNoise : public FractalMap
  22.   {
  23.   public:
  24.     NonNoise(const card order, const Seeds& seeds = FractalMap::Seeds(128),
  25.          const bits_per_pixel=8)
  26.       : FractalMap(order,seeds,bits_per_pixel) {}
  27.     inline int get_noise(const card scale) const { return 0; }
  28.   };
  29.  
  30.   {
  31.     const card seed = 128;
  32.     cout << "\tUniform seed " << seed << endl;
  33.     IMAGE map = NonNoise(order,seed,8);
  34.     verify_pixel_value(map,seed);
  35.   }
  36.    
  37.   {
  38.     const card seed = 128;        // Must be a power of 2
  39.     cout << "\tHorizontal gradient fill, seed " << seed << endl;
  40.     IMAGE map = NonNoise(order,FractalMap::Seeds(0,0,seed,seed),8);
  41.     class CheckMap : public PixelAction
  42.     {
  43.       double step;
  44.       void operation(GRAY& pixel)
  45.       {
  46.     const GRAY expected = (GRAY)(col * step);
  47.     if( col != ncols-1 && pixel != expected )
  48.       _error("at (%d,%d), expected %d, found %d",
  49.          row,col,expected,pixel);
  50.       }
  51.       public: CheckMap(const card seed, const card order) : 
  52.     step((double)seed/(1<<order)) {}
  53.     };
  54.     
  55. //    map.print("map"); exit(0);
  56.     map.apply(CheckMap(seed,order));
  57.   }
  58.  
  59.   {
  60.     const card seed = 128;        // Must be a power of 2
  61.     cout << "\tVertical gradient fill, seed " << seed << endl;
  62.     IMAGE map = NonNoise(order,FractalMap::Seeds(0,seed,0,seed),8);
  63.     class CheckVMap : public PixelAction
  64.     {
  65.       double step;
  66.       void operation(GRAY& pixel)
  67.       {
  68.     const GRAY expected = (GRAY)(row * step);
  69.     if( row != nrows-1 && pixel != expected )
  70.       _error("at (%d,%d), expected %d, found %d",
  71.          row,col,expected,pixel);
  72.       }
  73.       public: CheckVMap(const card seed, const card order) : 
  74.     step((double)seed/(1<<order)) {}
  75.     };
  76.     
  77. //    map.print("map"); exit(0);
  78.     map.apply(CheckVMap(seed,order));
  79.   }
  80.  
  81.   cout << "\nDone" << endl;
  82. }
  83.  
  84. static void test_norandom(void)
  85. {
  86.   const card order = 2;
  87.   cout << "\nTest constructing a map of order " << order << 
  88.       " with non-random noise\n" << endl;
  89.  
  90.   class NoRNoise : public FractalMap
  91.   {
  92.   public:
  93.     NoRNoise(const card order, const Seeds& seeds = FractalMap::Seeds(128),
  94.          const bits_per_pixel=8)
  95.       : FractalMap(order,seeds,bits_per_pixel) {}
  96.     inline int get_noise(const card scale) const { return scale/2; }
  97.   };
  98.  
  99.   const card seed = 0;
  100.   cout << "\tUniform seed " << seed << endl;
  101.   IMAGE map = NoRNoise(order,seed,8);
  102.   map.print("map");
  103.  
  104.   IMAGE expected(map);
  105.   expected = 2; expected(0,0) = 0;
  106.   expected.square_of(3,rowcol(1,1)) = 4;
  107.   assert( map == expected );
  108.  
  109.   cout << "\nDone" << endl;
  110. }
  111.  
  112.                 // Show off maps
  113. static void demo_maps(void)
  114. {
  115.   cout << "Showing off different maps" << endl;
  116.  
  117.   class GaussNoise : public FractalMap
  118.   {
  119.   public:
  120.     GaussNoise(const card order, const Seeds& seeds,
  121.            const bits_per_pixel=8)
  122.       : FractalMap(order,seeds,bits_per_pixel) {}
  123.                 // Well-known Gaussian random number generator
  124.                 // Note rand() returns a number [0,2^15-1],
  125.                 // that is, within [0,1] with 15-bit implicit
  126.                 // fraction
  127.     inline int get_noise(const card scale) const {
  128.       long sum = 0;
  129.       for(register int i=0; i<12; i++)
  130.     sum += rand();            // keep the result within 
  131.       return (scale * (sum-(6<<15)))>>17; }    // [-scale/2,scale/2]
  132.   };
  133.  
  134.   LookupT sine_trans(LookupT::Identical(8));    // For some visual effect
  135.   {
  136.     for(register int i=sine_trans.q_min(); i<=sine_trans.q_max(); i++)
  137.       sine_trans[i] = (GRAY)((sin((i * M_PI)/128)+1)*128 - 1);
  138.   }
  139.  
  140.   for(;;)
  141.   {
  142.     cout << "Enter the map's order (log2 of map's dimension) (0 to quit)" 
  143.       << endl;
  144.     int order;
  145.     cin >> order;
  146.     if( order <= 1 )
  147.       return;
  148.     cout << "Select random number generator: 0 - Uniform, 1 - Gaussian"
  149.          << endl;
  150.     int type;
  151.     cin >> type;
  152.     cout << "Enter the four seeds " 
  153.       "(at upper-left, lower-left, upper-right, lower-right corners)" << endl;
  154.     int sul, sll, sur, slr;
  155.     cin >> sul >> sll >> sur >> slr;
  156.     IMAGE map = type == 0 ? 
  157.       (LazyImage&)FractalMap(order,FractalMap::Seeds(sul,sll,sur,slr)) :
  158.       (LazyImage&)GaussNoise(order,FractalMap::Seeds(sul,sll,sur,slr));
  159.     cout << "Select post-processing option" << endl;
  160.     cout << "\t0 - no postprocessing" << endl;
  161.     cout << "\t1 - apply blurring" << endl;
  162.     cout << "\t2 - apply a sine function" << endl;
  163.     cin >> type;
  164.     if( type == 1 )
  165.       FilterIt(map).conv(conv_kernel(1,1,1,CommonDenom(3))).
  166.     clip_to_intensity_range();
  167.     else if( type == 2 )
  168.       FilterIt(map).translate(sine_trans, LookupT::CoerceFringes);
  169.  
  170.     char buffer[70];
  171.     sprintf(buffer,"Seeds(%d,%d,%d,%d)",sul,sll,sur,slr);
  172. //    map.write_pgm("/tmp/abc",buffer);
  173. //    system("/usr/local/bin/X11/xv /tmp/abc");
  174.     map.display(buffer);
  175.   }
  176. }
  177.  
  178. main(void)
  179. {
  180.  test_nonoise(4); 
  181.  test_nonoise(9); 
  182.  test_norandom();
  183.  
  184. #ifdef __MWERKS__                // Macintosh
  185.   GetDateTime((unsigned long *)&qd.randSeed);
  186.   Random(); Random(); Random(); Random();    // Just randomizing
  187. #else
  188.   srand(time(0));
  189.   rand(); rand(); rand(); rand();
  190. #endif
  191.  
  192.  demo_maps();
  193. }
  194.